package booyah.validation;

import booyah.application.Event;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;

public class EventValidator {
    public static void validate(Event event, Errors errors) {
        if (!StringUtils.hasLength(event.getName())) {
            errors.rejectValue("name", "required", "required");
        }
        if (!StringUtils.hasLength(event.getDescription())) {
            errors.rejectValue("description", "required", "required");
        }
        if (!StringUtils.hasLength(event.getLocation())) {
            errors.rejectValue("location", "required", "required");
        }
        if (event.getStartTime() == null) {
            errors.rejectValue("startTime", "required", "required");
        }
        if (event.getEndTime() == null) {
            errors.rejectValue("endTime", "required", "required");
        }
        if ((event.getStartTime() != null) && (event.getEndTime() != null)) {
            if (event.getStartTime().after(event.getEndTime())) {
                errors.rejectValue("startTime", "required", "after End Time");
                errors.rejectValue("endTime", "required", "before Start Time");
            }
        }

    }
}
